$(function () {
    $('.allcat').hover(function () {
        console.log(1111)
        $('.blist').css({ 'display': 'block' })

    }, function () {

        $('.blist').css({ 'display': 'none' })
    })
    $('.menus').hover(function () {
        $('.uli').css({ 'display': 'block' })
    }, function () {
        $('.uli').css({ 'display': 'none' })
    })
    $('.menus1').hover(function () {
        $('.uli1').css({ 'display': 'block' })
    }, function () {
        $('.uli1').css({ 'display': 'none' })
    })

    // 退出按钮事件
    $('.tc').click(function () {
        localStorage.removeItem('token')
        localStorage.removeItem('username')
        location.href = 'login.html'
    })
    $('#yh').html(username)
    // 退出按钮事件
})














let token = localStorage.getItem('token')
console.log(token)
let username = localStorage.getItem('username')
// let oncli = document.querySelector('.on-cli')
// let areg = document.querySelector('.a-reg')
// let yonghu = document.querySelector('#yonghu')
// let btn = document.querySelector('.btn')
//如果已经登录 第一个div 显示
if (token) {
    $('.reg').hide()
    $('.login').hide()
    $('.use').show()
    // oncli.style.display = 'none'
    // areg.style.display = 'none'
    // yonghu.style.display = 'block'
    // btn.style.display='block'

} else {
    // oncli.style.display = 'block'
    // areg.style.display = 'block'
    // yonghu.style.display = 'none'
    // btn.style.display='none'
    $('.reg').show()
    $('.login').show()
    $('.use').hide()
}


let url = new URL(location.href)
console.log(url.searchParams)
console.log(url.searchParams.get('pid'))
console.log(location)
let id = url.searchParams.get('pid')
$.get('http://47.104.244.134:8080/goodsbyid.do', { id: id }).done(data => {
    console.log(data)
    let p = data;




    let desc01 = document.querySelector('.desc-01')
    console.log(desc01)
    desc01.innerHTML = p.info

    let name = document.querySelector('.name')
    let desc = document.querySelector('.desc')
    desc.innerHTML = p.info
    name.innerHTML = p.name
    let price02 = document.querySelector('.price-02')
    price02.innerHTML = p.price


    let imgs = document.querySelector('.imgs img')
    imgs.src = p.picurl
    console.log(imgs)

    let imglist = document.querySelector('.img-list img')
    imglist.src = p.picurl
})

















//加入购物车的使用  addGoodsApi 接口 
//给加入购物车按钮加点击事件 调用接口
let btt = document.querySelector('.btt')
console.log(btt)
let pnumIpt = document.querySelector('.pnum')
console.log(pnumIpt)
btt.onclick=function () {
    let gid = id
    console.log(gid)
    let token = localStorage.getItem('token')
    let pnum = pnumIpt.value;//获取文本框商品数量
    let params = { gid, token, pnum }
    $.get('http://47.104.244.134:8080/cartsave.do', { gid: id, token: token, count: pnum }).done(data => {

        console.log(data)
        alert('添加成功')
        location.href = 'cart.html'
    })
}



//加减按钮
let addBtn = document.querySelector('.add')
console.log(addBtn)
let subBtn = document.querySelector('.sub')
addBtn.onclick = function () {

    pnumIpt.value = parseInt(pnumIpt.value) + 1

    console.log(pnumIpt.value)

}
subBtn.onclick = function () {
    console.log(pnumIpt.value)
    if (pnumIpt.value == 1) {
        return;
    }
    pnumIpt.value = parseInt(pnumIpt.value) - 1
}

let cartadd = document.querySelector('.cart-add')
cartadd.onclick = function () {
    location.href = 'https://mall.sogou.com/goods/order/133403559302529024'
}
